package com.mashibing.tank.chainofresponsibility;

import com.mashibing.tank.AbstractGameObject;
import com.mashibing.tank.PropertyMgr;

import java.util.ArrayList;
import java.util.List;

/**
 * @description:
 * @author: 余希瑶
 * @date: 2021年06月23日 15:19
 * @version:1.0
 */
public class ColliderChain implements Collider {
    List<Collider> colliders;

    public ColliderChain() {
        //初始化碰撞器
        initColliders();
    }

    private void initColliders() {
        colliders = new ArrayList<>();
        String[] collidersNames = PropertyMgr.get("colliders").split(",");

        try {
            for (String name : collidersNames) {
                Class clazz = Class.forName("com.mashibing.tank.chainofresponsibility." + name);
                Collider c = (Collider) (clazz.getConstructor().newInstance());
                colliders.add(c);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    public boolean collide(AbstractGameObject go1, AbstractGameObject go2) {
        for (Collider collider : colliders) {
            if (!collider.collide(go1, go2)) {
                return false;
            }
        }
        return true;
    }
}
